package com.briup.ex;



import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 使用javaGUI 完成随机点名程序
 *
 */
public class RandomFrame  extends JFrame {
    List <Student>list=null;
   Random r= new  Random();

    void  readFile(){
        try(FileReader fr = new FileReader("C:\\Users\\sxx\\IdeaProjects\\bttc-java-basic-code\\02code\\src\\com\\briup\\ex\\students.txt"); BufferedReader br = new BufferedReader(fr)){
            list=br.lines()
                    .map(i->i.split("\t"))
                    .map(i->new Student(i[0],i[1]))
                    .collect(Collectors.toList());

       }catch (Exception e){

       }
    }

    public void   addComponent(){
        //创建布局
        BorderLayout layout = new BorderLayout();
        //创建面板并指定布局
        JPanel panel = new JPanel(layout);
        JButton button = new JButton("开始点名",new ImageIcon("C:\\Users\\sxx\\IdeaProjects\\bttc-java-basic-code\\02code\\src\\com\\briup\\ex\\1.png"));
        TextArea area = new TextArea();
        //设置文本域你不允许输入
        area.setEditable(false);
        area.setBackground(Color.green);
        area.setFocusable(true);
        area.setFont(new Font("黑体",Font.CENTER_BASELINE,50));
        //设置按钮的大小
        Font font = new Font("宋体", Font.BOLD, 36);
        button.setFont(font);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                final JButton but = (JButton) e.getSource();
                but.setText("点名中...");
                new  Thread(){
                    @Override
                    public void run() {

                        for (int i = 0; i <50 ; i++) {
                            List<Student> students=new ArrayList(list);
                            System.out.println(students.size());
                            StringBuilder sb=new StringBuilder("");
                            for (int j = 0; j < 10; j++) {
                                final Student stu1 = students.remove(r.nextInt(students.size()));
                                sb.append(stu1.toString());
                            }


                            area.setText(sb.toString());
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException ex) {
                                ex.printStackTrace();
                            }

                        }
                        but.setText("开始点名");
                    }
                }.start();
            }
        });
        panel.setBackground(Color.pink);
        //添加组件到面板
        panel.add(button,BorderLayout.NORTH);
        panel.add(area,BorderLayout.CENTER);
        //添加面板到窗体
        this.add(panel);

    }

    public RandomFrame(String title) throws HeadlessException {
        super(title);
        readFile();
        addComponent();
    }

    public static void main(String[] args) {
        //获取计算机屏幕大小
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        int height = (int) size.getHeight();
        int width = (int) size.getWidth();
        //创建窗体对象
        RandomFrame frame = new RandomFrame("随机点名程序");
        //设置窗体大小
        frame.setSize(400,600);
        //设置窗体定位
        frame.setLocation((width-400)/2,(height-600)/2);
        //设置窗体不允许拖拽更改大小
        frame.setResizable(false);
        //设置默认关闭
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //显示窗体
        frame.setVisible(true);

    }
}
